﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WindowsFormsencrapt
{
   /// <summary>
   /// 给女娲生成有效序列号的程序，把三个字母拆开BIT分散到每个字符里
   /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public List<string> rlist = new List<string>(); //只要没关程序，就保证不重复
        private void btTest_Click(object sender, EventArgs e)
        {
            int t = int.Parse(textType.Text);
            int num = int.Parse(textNum.Text);
            textResult.Text = "";
            string temp = "";
            int startIndex = rlist.Count;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < num; i++)
            {
                temp = Encrypt(textBox1.Text, t);
                while (rlist.Contains(temp))
                {
                    temp = Encrypt(textBox1.Text, t);
                }
                rlist.Add(temp);
                textBox2.Text= temp; 
            }
            int index = 1;
            textResult.Text = "";
            int j = 0;
            for (j = startIndex;j<rlist.Count ;j++)
            {
                sb.Append((index).ToString() + ":  " + rlist[j] + "\r\n");
                index++;
            }

            textResult.Text= sb.ToString();
            //int rest = 0;
            //textBox3.Text = Decrypt(textBox2.Text, ref rest);
            //textBox5.Text= rest.ToString();
        }
        //ct 0-3 作为加密的变位标志，让变化更多
        public string Encrypt(string plainText,int ct)
        {
            if (plainText.Length != 3)
            {
                throw new ArgumentException("The input string must be 3 characters long.");
            }

            StringBuilder sb = new StringBuilder();
            Random random = new Random();

            for (int i = 0; i < 24; i++)
            {

                int bitIndex = i % 8;
                char c = i < 8 ? plainText[0] : i < 16 ? plainText[1] : plainText[2];
                int bitValue = (c >> bitIndex) & 1;

                //string validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                string validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                List<char> matchingChars = new List<char>();

                for (int j = 0; j < validChars.Length; j++)
                {
                    int temp = (int)validChars[j];
                    if (((temp >> bitIndex) & 1) == bitValue)
                    {
                        matchingChars.Add(validChars[j]);
                    }
                }
                if ((i == 7)|| (i == 15))
                {
                    matchingChars = matchingChars.Where(o =>((int)o<89)&& ((int)o& 1) == 0).ToList();
                }

                char encryptedChar = matchingChars[random.Next(matchingChars.Count)];
                if (i == 7)
                {
                    encryptedChar = (char)((int)encryptedChar + (ct&1));
                }else if(i==15)
                {
                    encryptedChar = (char)((int)encryptedChar + ((ct>>1) & 1));
                }
                sb.Append(encryptedChar);
            }

            return AddCross(sb.ToString());
        }

        public string Decrypt(string encryptedText, ref int ct)
        {
            encryptedText=encryptedText.Replace("-", "");
            if (encryptedText.Length != 24)
            {
                throw new ArgumentException("The input string must be 24 characters long.");
            }
            ct = 0;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 3; i++)
            {
                int charValue = 0;
                for (int j = 0; j < 8; j++)
                {
                    int bitIndex = i * 8 + j;
                    char encryptedChar = encryptedText[bitIndex];
                    int bitValue = (encryptedChar >> j) & 1;
                    charValue += bitValue << j;

                    if (i == 0 && j == 7)
                    {
                        ct = encryptedChar & 1;
                    }
                    else if (i == 1 && j == 7)
                    {
                        ct += (encryptedChar & 1) << 1;
                    }
                }  

                sb.Append((char)charValue);
            }

            return sb.ToString();
        }

        public string AddCross(string srcTet)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < srcTet.Length; i += 4)
            {
                sb.Append(srcTet.Substring(i, Math.Min(4, srcTet.Length - i)));
                if (i + 4 < srcTet.Length)
                {
                    sb.Append("-");
                }
            }
            return  sb.ToString();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            int rest = 0;
            textBox3.Text = Decrypt(textBox2.Text, ref rest);
            textBox5.Text= rest.ToString();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }


    }
}
